import { defineStore } from "pinia";
import { ref } from "vue";
import { getCurrentUser } from "@/api/user";

export const useLoginUserStore = defineStore("loginUser", () => {
  const loginUser = ref<any>({
    name: "未登录",
  });

  async function fetchGetLoginUser() {
    const res = await getCurrentUser();
    if (res.data.code == 200 && res.data.data) {
      loginUser.value = res.data.data;
    } else {
      setTimeout(() => {
        loginUser.value = { name: "小黑子", id: "1" };
      }, 3000);
    }
  }

  function setLoginUser(newLoginUser: any) {
    loginUser.value = newLoginUser;
  }

  return { loginUser, fetchGetLoginUser, setLoginUser };
});
